package com.example.leo.mycharmingstudent.a0915;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.leo.mycharmingstudent.R;

public class IntentActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "IntentActivity";
    public static final String MY_HIDE_ACTION = "android.intent.my.HIDE";
    public static final String MY_HIDE_CATEGORY = "android.intent.category.HIDE";

    //Activity隐式启动按钮
    private Button btnHideIntent;
    //Activity显式启动按钮
    private Button btnShowIntent;
    //打电话按钮
    private Button btnCall;
    //打开网页按钮
    private Button btnWeb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);
        btnHideIntent = findViewById(R.id.btn_hide_intent_activity_intent);
        btnShowIntent = findViewById(R.id.btn_show_intent_activity_intent);
        btnCall = findViewById(R.id.btn_call_activity_intent);
        btnWeb = findViewById(R.id.btn_web_activity_intent);

        btnHideIntent.setOnClickListener(this);
        btnShowIntent.setOnClickListener(this);
        btnCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打电话
                Intent intent = new Intent();
                //使用ACTION_VIEW这个action，可以打开拨号界面
                intent.setAction(Intent.ACTION_VIEW);
                /*
                Intent.ACTION_DIAL 打开拨号界面，不需要权限
                Intent.ACTION_CALL 直接拨号  牵扯隐私，权限需要动态申请
                * */
                /*intent.setAction(Intent.ACTION_DIAL);
                intent.setAction(Intent.ACTION_CALL);*/
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
            }
        });
        btnWeb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开网页
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_hide_intent_activity_intent:
                //隐式意图
                Intent intent = new Intent();
                intent.setAction(MY_HIDE_ACTION);
                intent.addCategory(MY_HIDE_CATEGORY);
                startActivity(intent);
                break;
            case R.id.btn_show_intent_activity_intent:
                //显式意图  明明白白地看出，启动IntentActivity
                startActivity(new Intent(this, IntentActivity.class));
                break;
            default:
                Log.e(TAG, "onClick: What are you 弄啥嘞？");
                break;
        }
    }
}
